$(document).ready(function () {
    var roleName = $("#loadRoleId", parent.document).val();


});


/**
 * 动态设置iframe高度
 */
function function_iframe_init() {
    var bodyHeight = $("#function_list_body").height();
    $(this.parent.document).find("#function_list_iframe").height(bodyHeight);
}

/**
 * 权限名-鼠标悬停
 * @param _this
 */
var layOb;

function mouseover(_this) {
    layOb = layer.tips(_this.getAttribute("value"), _this, {tips: [1, '#000000']});
}

/**
 * 权限名-鼠标移开
 */
function mouseout() {
    layer.close(layOb);
}

/**
 * 权限名-双击事件
 * @param _this
 */
var textBak;

function dblclick(_this) {
    //原文本值
    textBak = _this.innerText;
    var controlUrl = _this.getAttribute("value");

    //创建元素对象
    var newDocOb = document.createElement("input");
    newDocOb.setAttribute("id", "clickInput");
    newDocOb.setAttribute("type", "text");
    newDocOb.setAttribute("value", textBak);
    newDocOb.setAttribute("class", "input-text radius");
    newDocOb.setAttribute("onChange", "inputOnChange()");
    newDocOb.setAttribute("onBlur", "inputOnBlur()");
    newDocOb.setAttribute("controlUrl", controlUrl);
    newDocOb.style.width = "120px";

    //将原值清空
    _this.innerText = "";

    //将新对象放入
    _this.appendChild(newDocOb);

    newDocOb.focus();

}


/**
 * 输入框失去焦点事件
 */
function inputOnBlur() {
    updateFunctionName();
    $("#delDiv").remove();
}

/**
 * 回车
 */
function inputOnChange() {
    updateFunctionName();
    $("#delDiv").remove();
}

/**
 * 更新权限名称方法
 */
var lastTime = "";

function updateFunctionName() {
    //获取输入框文本值
    var inputValue = $("#clickInput").val();
    var controlUrl = $("#clickInput").attr("controlUrl");

    //span赋值,同时清除input输入框
        $("#clickInput").parent().text(inputValue);

    if (inputValue == textBak) {
        return;
    }
    var nowTime = new Date();
    if ("" != lastTime && (nowTime - lastTime) < 100) {
        return;
    }
    lastTime = new Date();

    var param = {
        "functionName": inputValue,
        "controlUrl": controlUrl
    }
    //修改数据库权限名
    $.ajax({
        type: "POST",
        url: "/account/function/updateByUrl",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(param),
        dataType: "json",
        success: function (result) {
            if (result.success) {
                layer.msg("禁用成功!")
            } else {
                document.location.reload(true);
                layer.msg("禁用失败:" + result.cause);
            }
        },
        error: function (result) {
            document.location.reload(true);
            layer.msg("禁用失败:" + result.cause);
        }
    });
}

/**
 * 功能悬停
 */
function functionEnter(_this){

    _this.style.backgroundColor = "#C1C1C1";

    //创建删除div
    var newDelDiv = document.createElement("div");
    newDelDiv.setAttribute("id", "delDiv");
    newDelDiv.setAttribute("onmouseover", "delOnOver(this)");
    newDelDiv.setAttribute("onmouseout", "delOnOut(this)");
    newDelDiv.setAttribute("onclick", "delClick(this)");
    newDelDiv.style.position = "absolute";
    newDelDiv.style.right = "15px";
    newDelDiv.style.top = "4px";

    //创建删除图标
    var newDelicon = document.createElement("i");
    newDelicon.setAttribute("class", "icon Hui-iconfont Hui-iconfont-close");

    //添加标签元素
    newDelDiv.appendChild(newDelicon);
    _this.appendChild(newDelDiv);
}

/**
 * 功能移开
 */
function functionLeave(_this){
    _this.style.backgroundColor = "";
    $("#delDiv").remove();
}

/**
 * 删除悬停
 */
function delOnOver(_this){
    _this.style.backgroundColor = "#AAAAAA";

}

/**
 * 删除移开
 */
function delOnOut(_this){
    _this.style.backgroundColor = "";
}

/**
 * 删除(禁用)功能
 */
function delClick(_this){
    _this.parentNode.parentNode.removeChild(_this.parentNode);

    var controlUrl = _this.parentNode.getElementsByTagName("span")[0].getAttribute("value");

    var param = {
        "status": 0,
        "controlUrl": controlUrl
    }
    //更新功能权限状态
    $.ajax({
        type: "POST",
        url: "/account/function/updateByUrl",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(param),
        dataType: "json",
        success: function (result) {
            if (result.success) {
                layer.msg("禁用成功!")
            } else {
                document.location.reload(true);
                layer.msg("禁用失败:" + result.cause);
            }
        },
        error: function (result) {
            document.location.reload(true);
            layer.msg("禁用失败:" + result.cause);
        }
    });
}